package com.lagou.servlet;


import com.lagou.service.StudentService;
import com.lagou.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "StudentDeleteServlet", urlPatterns = "/studentDeleteServlet")
public class StudentDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式， 解决中文乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");

        // 1、获取前台得到的ids数据内容
        String idStr = request.getQueryString();
        idStr = idStr.replace("id=","");
        String[] ids = idStr.split("&");
        // 2、通过Service层实现对应功能
        StudentService studentService =  new StudentServiceImpl();
        int res = 0;
        res = studentService.studentDeleteService(ids);
        // 3、通过输出流向前台页面展示处理结果， 然后重新请求一次数据
        PrintWriter writer = response.getWriter();
        //获取模块名
        String contextPath = this.getServletContext().getContextPath();
        //拼接请求路径
        String hrefPath = contextPath + "/studentPageQueryServlet";
        if(0 != res){
            writer.print("<script> alert('管理学生信息成功！'); location.href='"+ hrefPath +"';</script>");
        } else {
            writer.print("<script> alert('管理学生信息失败！'); location.href='"+ hrefPath +"';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
